package com.cenntro.nearr.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.cenntro.nearr.base.BaseMapper;
import com.cenntro.nearr.base.BaseServiceImpl;
import com.cenntro.nearr.mapper.EmployeeMapper;
import com.cenntro.nearr.redis.IRedisService;
import com.cenntro.nearr.service.EmployeeService;
import com.cenntro.nearr.util.MessageDigestUtil;
import com.cenntro.nearr.vo.EmployeeVO;


@Service
public class EmployeeServiceImpl extends BaseServiceImpl<EmployeeVO> implements EmployeeService{

	private Logger LOG = org.slf4j.LoggerFactory.getLogger(EmployeeServiceImpl.class);
	
	@Resource
	private EmployeeMapper empMapper;
	@Resource
	private IRedisService iredisService;
	
	@Override
	public BaseMapper<EmployeeVO> getMapper() {
		return empMapper;
	}
	
	public EmployeeVO selectByPhone(String phone, String password){
		return empMapper.selectByPhone(phone, password);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public Boolean login(EmployeeVO emp){
		LOG.info("param:" + JSON.toJSONString(emp));
		EmployeeVO emp1 = this.selectByPhone(emp.getTelephone(), emp.getPassword());
		if (emp1 == null){
			return Boolean.FALSE;
		}
		String token = MessageDigestUtil.MD5(emp.getTelephone());
		emp1.setToken(token);
		empMapper.update(emp1);
		emp.setToken(token);
		
		iredisService.set(token, emp.getTelephone());
		return Boolean.TRUE;
	}
}
